package com.example.config.security.service;

import com.example.entity.Permission;
import com.example.entity.User;
import com.example.service.PermissionService;
import com.example.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户认证处理器类
 */
@Component
public class CustomerUserDetailsService implements UserDetailsService {

    @Resource
    private UserService userService;

    @Resource
    private PermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        User user = userService.findUserByUserName(username);
        //对象为空则认证失败
        if (user == null){
            throw new UsernameNotFoundException("用户名或密码错误！");
        }

        //查询用户所拥有的权限
        List<Permission> permissionList = permissionService.findPermissionListByUserId(user.getId());

        //获取权限编码
        List<String> codes = permissionList.stream().filter(item -> item != null)
                .map(item -> item.getCode())
                .filter(item -> item!= null)
                .collect(Collectors.toList());

        //转换成数组
        String[] strings = codes.toArray(new String[codes.size()]);

        //设置权限列表
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(strings);
        user.setAuthorities(authorityList);

        //设置菜单列表
        user.setPermissionList(permissionList);
        return user;
    }
}
